// pages/my/order.js
import api from '../../api/index'
import { userInfoStore } from '../../store/userInfo'
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:99,
    dataList: [],
    uploading: false,
    page:1,
    current: 1,
    total: 0,
    pages: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    this.setData({ dataList: [], uploading: false}, () => {
      self.getList();
    })
  },

  onShow: function(){
    
  },

  selectItem:function(e){
    let self=this;
    this.setData({ page: 1, dataList: [], uploading: false, status: e.currentTarget.dataset.status},()=>{
      self.getList();
    });
  },

  getList: function (){
    //openId: 'ofhiR4o14HgFB0zd2RYJ-pjC0trQ',
    
    let self=this;
    let param = {
      openId: userInfoStore.userInfo.openId,
      page:this.data.page,
      pageSize:20
    }

    if (this.data.status!=99){
      param = Object.assign(param, { orderStatus: this.data.status })
    }

    api.getshopreceiveorderlist(param).then(res => {
      console.log(res);
      let { result } = res;
      if (self.data.uploading) {
        if (self.data.page == self.data.pages) {
          let number = self.data.total - self.data.current * 20;
          let len = result.records.length;
          for (var i = (len - number); i < len; i++) {
            self.data.dataList.push(result.records[i]);
          }
        } else {
          self.data.dataList.push(...result.records);
        }
        self.setData({
          dataList: self.data.dataList,
          current: result.current,
          total: result.total,
          pages: result.pages,
          uploading: false
        })

      } else {
        self.setData({
          dataList: result.records,
          current: result.current,
          total: result.total,
          pages: result.pages,
          uploading: false
        })
      }
      
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self=this;
    this.setData({ page: 1, dataList: [], uploading:false},()=>{
      self.getList();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.uploading && this.data.current < this.data.pages) {
      let self=this;
      let next = this.data.current + 1;
      this.setData({ page:next, uploading: true },()=>{
        self.getList();
      });
      
    }
  }
})